package com.han.spider.safecounty;

import com.han.util.UnZipper;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

/**
 * @Author: Hanjiafeng
 * @Date: 2020-4-16 08:25:50
 * @Desc: 添加新的视频监控节点
 */
public class VideoMonitorNew extends VideoMonitor {

    public VideoMonitorNew(String cookie) {
        super(cookie);
    }

    public static void main(String[] args) {
        VideoMonitorNew videoMonitorNew = new VideoMonitorNew("account=15305672778; safeVillageCookie=safeVillageCookie_9e258de9-4f23-4689-a77b-4d7248a0883b192.168.1.29");
        videoMonitorNew.union();
    }

    private void insertVillage(String superiorId, String name) throws IOException, InterruptedException {

        HttpRequest.BodyPublisher postData = HttpRequest.BodyPublishers.ofString("reginId=" + superiorId + "&name=" + name);
        HttpRequest httpRequest = getRequestBuilder()
                .uri(URI.create("https://ehome.21cn.com/cameraPlatform/mapi/group/addGroup"))
                .POST(postData)
                .build();

        HttpResponse<byte[]> byteResponse = getHttpClient().send(httpRequest, HttpResponse.BodyHandlers.ofByteArray());
        String body = new String(UnZipper.unGzipByBytes(byteResponse.body()), StandardCharsets.UTF_8);
        System.out.println(body);
    }

    /**
     * 逐级查找然后将庄单独建立
     */
    private void union() {
        try {
            Map<String, List<String[]>> townMap = selectRegion("40485");
            townMap.forEach((superiorId, townList) -> townList.forEach(town -> {
                try {
                    Map<String, List<String[]>> villageMap = selectRegion(town[0]);
                    villageMap.forEach((townId, villageList) -> villageList.forEach(village -> {
                        try {
                            Map<String, List<String[]>> zhuangMap = selectRegion(village[0]);
                            zhuangMap.forEach((villageId, zhuangList) -> zhuangList.forEach(zhuang -> {
                                System.out.print("将" + zhuang[1] + "放到" + village[1] + "下");
                                String expect = "楼山村";
                                try {
                                    if (!expect.equals(village[1])) {
                                        insertVillage(villageId, zhuang[1]);
                                        System.out.println("完成");
                                    }
                                } catch (IOException | InterruptedException e) {
                                    e.printStackTrace();
                                    System.out.println("失败");
                                }
                            }));
                        } catch (IOException | InterruptedException e) {
                            e.printStackTrace();
                        }
                    }));
                } catch (IOException | InterruptedException e) {
                    e.printStackTrace();
                }
            }));
        } catch (InterruptedException | IOException e) {
            e.printStackTrace();
        }
    }
}
